package com.chenjt.juc.six_LockSupport;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/7/8 11:41
 */
public class LockSupportDemo {

    public static void main(String[] args) {

//        syncWaitAndNotify();

//        lockAwaitAndSignal();

        Thread t1 = new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"\t ----come in");
            LockSupport.park();
            System.out.println(Thread.currentThread().getName()+"\t ----被唤醒");
        },"t1");
        t1.start();

        /*try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/

        new Thread(()->{
            LockSupport.unpark(t1);
            System.out.println(Thread.currentThread().getName()+"\t ----发出通知");
        },"t2").start();


    }


    private static void lockAwaitAndSignal() {
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        new Thread(()->{
            try {
                // 这里需要lock.lock()和lock.unlock()，condition.await()和signal()才可以执行不会报错
                lock.lock();
                System.out.println(Thread.currentThread().getName()+"\t ----come in");
                condition.await();
                System.out.println(Thread.currentThread().getName()+"\t ----被唤醒");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        },"t1").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            lock.lock();

            try {
                condition.signal();
                System.out.println(Thread.currentThread().getName()+"\t ----发出通知");
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        },"t2").start();

    }

    private static void syncWaitAndNotify() {

        Object objectLock = new Object();

        new Thread(()->{
            // objectLock 需要加 synchronized才能用wait()和notify()
            synchronized (objectLock){
                System.out.println(Thread.currentThread().getName()+"\t ----come in");

                try {
                    objectLock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName() + "\t ----被唤醒");
            }
        },"t1").start();


        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
            synchronized (objectLock){
                objectLock.notify();
                System.out.println(Thread.currentThread().getName()+"\t ----发出通知");
            }
        },"t2").start();
    }

}
